package com.billy.android.loadingstatusview

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.billy.android.loading.DefaultCustomAdapter
import com.billy.android.loading.Gloading
import com.billy.android.loadingstatusview.databinding.ActivityCustomViewBinding
import com.billy.android.loadingstatusview.util.Util
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target

/**
 * Created by kermitye on 2022/4/21 10:11
 * desc:
 */
class CustomViewActivity: AppCompatActivity() {

    val binding by lazy { ActivityCustomViewBinding.inflate(layoutInflater) }

    var picUrl = ""

    //状态页管理类
    val stateHolder by lazy {

        //如果使用默认的话需要在Applictaion初始化的时候统一设置默认的adapter
//        Gloading.getDefault()
        Gloading.from(DefaultCustomAdapter()).wrap(binding.content)
            .setLoadingView(getLoadingView())
            .setEmptyView(getDefaultStatusView(R.drawable.icon_empty, "没有数据", "请重试") {
                onLoadRetry("https://test.hhh")
            })
            .setErrorView(
                getDefaultStatusView(
                    R.drawable.icon_failed, "加载失败，点击重试", "重新加载"
                ) { onLoadRetry(picUrl = Util.getRandomImage()) })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.content.setOnClickListener {
            initData()
        }
        initData()
    }


    fun initData() {
        //模拟加载数据
        stateHolder.showLoading()
        binding.content.postDelayed({
            stateHolder.showEmpty()
        }, 2000)
    }




    fun onLoadRetry(picUrl: String) {
        stateHolder.showLoading()
        Glide.with(this)
            .load(picUrl)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    stateHolder.showLoadFailed()
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    stateHolder.showLoadSuccess()
                    return false
                }

            })
            .into(binding.iv)
    }


    protected fun getLoadingView(): View? {
        return LayoutInflater.from(this).inflate(R.layout.view_loading, null, false)
    }

    protected fun getDefaultStatusView(
        resId: Int,
        msg: String?,
        retryBtn: String?,
        retryListener: View.OnClickListener?
    ): View? {
        val customView = LayoutInflater.from(this).inflate(R.layout.view_status_custom, null, false)
        if (retryListener != null) {
            customView.findViewById<View>(R.id.btnRetry).setOnClickListener(retryListener)
        }
        if (resId != -1) {
            (customView.findViewById<View>(R.id.image) as ImageView).setImageResource(resId)
        }
        if (!TextUtils.isEmpty(msg)) {
            (customView.findViewById<View>(R.id.text) as TextView).text = msg
        }
        (customView.findViewById<View>(R.id.btnRetry) as Button).visibility =
            if (TextUtils.isEmpty(retryBtn)) View.GONE else View.VISIBLE
        if (!TextUtils.isEmpty(retryBtn)) {
            (customView.findViewById<View>(R.id.btnRetry) as Button).text =
                retryBtn
        }
        return customView
    }
}